//
//  HMStatus.swift
//  Weibo
//
//  Created by 米易雅克 on 16/4/18.
//  Copyright © 2016年 米易雅克. All rights reserved.
//

import UIKit

class HMStatus: NSObject {
    
    //微博创建事件
    var created_at: String?
    
    //微博ID
    var id: Int64 = 0
    
    //微博信息内容
    var text: String?
    
    //微博来源
    var source: String? {
        didSet {
        //kvc给来源赋值，我们重新处理来源
            if let newSource = source {
                //截取字符串String
                guard let firstRange = newSource.rangeOfString("\">") else {
                
                    print("没有匹配\">")
                    return
                }
                guard let secondRange = newSource.rangeOfString("</a>") else {
                
                    print("没有匹配的</a>")
                    return
                }
                //拼接来源的范围“360安全浏览器”
                let range = firstRange.endIndex..<secondRange.startIndex
                //将截取的字符串重新给source赋值，在didSet重新给自己赋值，不会出发didset方法
                source = "来自 " + newSource.substringWithRange(range)
                
            }
        
        
        }
    
    }
    
    //微博配图/数组中存放的是字典
    var pic_urls: [[String: AnyObject]]? {
    
        didSet {
            let count = pic_urls?.count ?? 0
            
            if count == 0 {
                return
            }
            //创建storePicutreURLs数组保存NSURL
            storePictureURLs = [NSURL]()
            
            //有图片要显示时
            for dict in pic_urls! {
            // 获取到每个字典,解析thumbnail_pic所对应的value,value是一个图片的地址(String)
                let urlStrig = dict["thumbnail_pic"] as! String
                let url = NSURL(string: urlStrig)!
                
                storePictureURLs!.append(url)
            }
        }
    }
    
    //模型提供好用一点的图片NSURL数组
    var storePictureURLs: [NSURL]?
    //用户模型
    var user : HMUser?
    /// 被转发的原微博信息字段，当该微博为转发微博时返回
    var retweeted_status: HMStatus?
    //字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    // setValuesForKeysWithDictionary最终会调用这个方法,字典中有多少个key,就会调用多少次setValue:forKey:
    // value: 字典中的value, key: 字典中的key
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            if let dict = value as? [String: AnyObject] {
                
                user = HMUser(dict: dict)
            }
            return
        }
        // 拦截retweeted_status这个key，不要系统来KVC,我们自己来赋值
        // 1.key == retweeted_status, 2. value可以转成字典
        
        if key == "retweeted_status", let dict = value as? [String: AnyObject] {
           
            retweeted_status = HMStatus(dict: dict)
            
            return
            
        }
        super.setValue(value, forKey: key)
    }
    //字典中找不到key时调用当前方法
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //对象属性打印
   override var description: String{
    
        get{
        
            let keys = ["created_at","id","text","source","pic_urls","user","retweeted_status"]
            
            return "微博模型\n\t\t\(self.dictionaryWithValuesForKeys(keys).description)"
        }
    }
}
